<?php

namespace tencentcloud;

use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Tiw\V20190919\TiwClient;

class Tiw
{

  private $handler = null;

  /**
   * 实例化
   * @param array $options
   */
  public function __construct($options = [])
  {
    $opt = config('cloud.tencent');
    if (!empty($options)) $opt = array_merge($opt, $options);
    $cred        = new Credential($opt['secretId'], $opt['secretKey']);
    $httpProfile = new HttpProfile();
    $httpProfile->setEndpoint("tiw.tencentcloudapi.com");
    $clientProfile = new ClientProfile();
    $clientProfile->setHttpProfile($httpProfile);
    $this->handler = new TiwClient($cred, "ap-guangzhou", $clientProfile);
  }

  /**
   * 直接执行指定方法
   * @param $fun
   * @param array $params
   * @return mixed
   */
  public function execFun($fun, $params = [])
  {
    $res = $this->handler->$fun($this->fromJsonString($fun, $params));
    return json_decode($res->toJsonString(), true);
  }

  /**
   * 传输额外参数
   * @param $action
   * @param $params
   * @return mixed
   */
  public function fromJsonString($action, $params = [])
  {
    $respClass = "TencentCloud" . "\\" . "Tiw" . "\\" . "V20190919\\Models" . "\\" . ucfirst($action) . "Request";
    $obj       = new $respClass();
    $params    = json_encode($params, JSON_UNESCAPED_UNICODE);
    $obj->fromJsonString($params);
    return $obj;
  }

}